package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.widget.EditText;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.event.OperationRespEvent;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.net.handler.CommentHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.CommentReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.widget.CommonTitleBarView;
import com.hangzhou.xyj.utils.EventBusUtil;
import com.hangzhou.xyj.utils.StringUtil;
import com.hangzhou.xyj.utils.ToastUtil;
import com.hangzhou.xyj.utils.constants.DataKey;


/**
 * Created by Administrator on 2016/1/22.
 */
public class CommentActivity extends BaseActivity {
    EditText mEdComment;
    private String mContent;//评论内容
    private int id;//评论实体的ID
    private int mIntentTag;//标识从哪个页面进入

    @Override
    public void initIntentData(Intent intent) {
        id = intent.getIntExtra(DataKey.OBJ_ID, -1);
        mIntentTag = intent.getIntExtra(DataKey.INTENT_TAG, -1);
    }

    @Override
    public void initDatas() {
        mEdComment.requestFocus();
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_comment);
    }

    @Override
    public void initListeners() {
        setOnNavRightListener(new CommonTitleBarView.OnNavRightListener() {
            @Override
            public void onNavRight() {
                publishComment();//发布
            }
        });
    }

    @Override
    public void initTop() {
        enableTop(true);
        enableTitle(true, "评论");
        enableRightNav(true, "确定");

    }

    @Override
    public void initViews() {
        mEdComment = (EditText) findViewById(R.id.et_comment);
    }

    private void publishComment() {
        CommentReq commentReq;
        mContent = mEdComment.getText().toString().trim();//获取评论内容
        if (StringUtil.isEmpty(mContent)) {
            ToastUtil.show("评论内容不能为空");
            return;
        }
        switch (EIntentTagType.valueOf(mIntentTag)) {
            case TAG_HOT_SPOT:
                commentReq = new CommentReq(CommentReq.TYPE_ACTIVITY, mMyUserId, id, mContent);
                break;
            case TAG_DYNAMIC_DETAIL:
            default:
                commentReq = new CommentReq(CommentReq.TYPE_DYNAMIC, mMyUserId, id, mContent);
                break;
        }
        HttpLoader.getInstance().sendRequest(new CommentHandler(commentReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                if ((Boolean) model) {
                    OperationRespEvent respEvent = new OperationRespEvent();
                    respEvent.setIsSuccess(true);
                    EventBusUtil.postEvent(respEvent);
                    ToastUtil.show("评论成功");
                    CommentActivity.this.finish();
                }
            }
        }, 0));
    }
}
